﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYFamilyLibDataManager;

namespace HYRElectric.Dimension
{
	public partial class DlgWireAmountDimensions : System.Windows.Forms.Form
	{
		public DbFamily DbTag
		{
			get
			{
				return this.dbTag;
			}
			set
			{
				this.dbTag = value;
			}
		}

		public DlgWireAmountDimensions(UIApplication m_uiApp)
		{
			this.InitializeComponent();
			this.uiApp = m_uiApp;
		}

		private void GetDimensionType(int WireNum)
		{
			this.sysDB = FamilyLibDatabase.GetSqliteDB(SubjectType.System, this.uiApp.Application.VersionNumber);
			List<DbFamily> allFamilies = this.sysDB.CategoryTable["电气标记"]["电线标记"]["根数标记"].GetAllFamilies(false, CheckType.All);
			if (WireNum == 3)
			{
				using (List<DbFamily>.Enumerator enumerator = allFamilies.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DbFamily dbFamily = enumerator.Current;
						if (dbFamily.Name == "导线根数标记-三根")
						{
							this.DbTag = dbFamily;
							break;
						}
					}
					return;
				}
			}
			if (WireNum > 3)
			{
				foreach (DbFamily dbFamily2 in allFamilies)
				{
					if (dbFamily2.Name == "导线根数标记-多根")
					{
						this.DbTag = dbFamily2;
						break;
					}
				}
			}
		}

		private void DlgWireAmountDimensions_Load(object sender, EventArgs e)
		{
			this.xmlHelper.LoadXml(this.path, "Root");
			//base.Icon = Resource.YArchitechIcon;
			this.LoadDlgData();
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireAmountDimensions"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireAmountDimensions", "Params"));
			this.xmlHelper.SetAttribute("WireNum", this.txtWireNum.Text.Trim());
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireAmountDimensions", "Params"));
			string attribute = this.xmlHelper.GetAttribute("WireNum");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.txtWireNum.Text = attribute;
			}
		}

		private void DlgWireAmountDimensions_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.txtWireNum.Text))
			{
				MessageBox.Show("导线根数标注的根数不能为空！", "盈建科提示");
				this.txtWireNum.Focus();
				return;
			}
			if (Convert.ToInt32(this.txtWireNum.Text.Trim()) < 3)
			{
				MessageBox.Show("导线根数标注的根数必须大于2根！", "盈建科提示");
				this.txtWireNum.Focus();
				return;
			}
			this.GetDimensionType(Convert.ToInt32(this.txtWireNum.Text.Trim()));
			base.DialogResult = DialogResult.OK;
		}

		private void nupdWireNum_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "WireAmountDimensions.xml");

		private const string Root = "Root";

		private const string Node = "HYWireAmountDimensions";

		private const string ChildNode = "Params";

		private const string WireNum = "WireNum";

		private FamilyLibDatabase sysDB;

		private DbFamily dbTag;

		private UIApplication uiApp;
	}
}
